package com.fss.daoTestCases;

import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import com.fssDataModels.*;
import com.fss.user.dao.UserDaoImpl;
import com.fss.secondHand.dao.*;
import com.fss.experience.dao.*;

public class TestExperienceDaoImpl extends TestCase{
	public TestExperienceDaoImpl(
	        String name) {
	        super(name);
	    }
	
	private UserDaoImpl db = new UserDaoImpl();//connection to database
	private ExperienceDaoImpl db2 = new ExperienceDaoImpl();
	
		/*========test right-data cases:============================*/		 
	    public void testInsert() {	    		    		    		    	
	        TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);
	        
	        assertEquals(1,db2.insertExperience(newExperience));
	        	        
	        db2.deleteExperience(newExperience);
	        db2.deleteExperienceType(type.getTypeId());
	    }
	    
	    public void testUpdate() {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);	        	        	      
	        db2.insertExperience(newExperience);
	        
	        newExperience.setContent("content changed");
	        db2.updateExperience(newExperience);
	        
	        TbExperience updatedExperience = db2.getExperience(newExperience.getExperienceId());
	        
	        assertEquals("content changed",updatedExperience.getContent());
	        	        
	        db2.deleteExperience(newExperience);
	        db2.deleteExperienceType(type.getTypeId());
	        
	    }
	    
	    public void testDelete() {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);	        	        	      
	        db2.insertExperience(newExperience);
	        	             
	        assertEquals(1,db2.deleteExperience(newExperience));
	        db2.deleteExperienceType(type.getTypeId());
	    		        
	    }
	    
	    /*============comment-related test cases:==========================*/
	    public void testAddComment()
	    {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);	        	        	      
	        db2.insertExperience(newExperience);
	        
	        TbComment comment = new TbComment();	        
	        comment.setContent("test add comment");
	        comment.setCreatedTime(new Date());
	        comment.setGeneratorName("ansinh");
	        
	        TbComment comment2 = new TbComment();	        
	        comment2.setContent("test add comment");
	        comment2.setCreatedTime(new Date());
	        comment2.setGeneratorName("ansinh");
	        
	        assertEquals(1,db2.addComment(newExperience, comment));
	        assertEquals(1,db2.addComment(newExperience, comment2));
	        
	        db2.deleteComment(comment);
	        db2.deleteComment(comment2);
	        db2.deleteExperience(newExperience);
	        db2.deleteExperienceType(type.getTypeId());
	        
	        
	    }
	    public void testDeleteComment()
	    {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);	        	        	      
	        db2.insertExperience(newExperience);
	        
	        TbComment comment = new TbComment();	        
	        comment.setContent("test add comment");
	        comment.setCreatedTime(new Date());
	        comment.setGeneratorName("ansinh");
	        
	        TbComment comment2 = new TbComment();	        
	        comment2.setContent("test add comment");
	        comment2.setCreatedTime(new Date());
	        comment2.setGeneratorName("ansinh");
	        
	        db2.addComment(newExperience, comment);
	        db2.addComment(newExperience, comment2);
	        
	        assertEquals(1,db2.deleteComment(comment));
	        assertEquals(1,db2.deleteComment(comment2));
	        
	        db2.deleteExperience(newExperience);
	        db2.deleteExperienceType(type.getTypeId());	        	        	        	       
	    	
	    }
	    public void testGetComments()
	    {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperience newExperience = new TbExperience(type,author,"dummy experience",new Date(),"no content",0);	        	        	      
	        db2.insertExperience(newExperience);
	        
	        TbComment comment = new TbComment();	        
	        comment.setContent("test add comment");
	        comment.setCreatedTime(new Date());
	        comment.setGeneratorName("ansinh");
	        
	        TbComment comment2 = new TbComment();	        
	        comment2.setContent("test add comment");
	        comment2.setCreatedTime(new Date());
	        comment2.setGeneratorName("ansinh");
	        
	        db2.addComment(newExperience, comment);
	        db2.addComment(newExperience, comment2);
	        	        	        
	        List comments = db2.getComments(newExperience);
	        
	        
	        assertEquals(2,comments.size());
	        
	        db2.deleteComment(comment);
	        db2.deleteComment(comment2);
	        db2.deleteExperience(newExperience);
	        db2.deleteExperienceType(type.getTypeId());
	        
	    	
	    }
	    
	    public void testGetExperienceType()
	    {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        db2.insertExperienceType(type);
	        
	        TbExperienceType storedType = db2.getExperienceType(type.getTypeId());
	        
	        assertEquals(storedType.getTypeName(),type.getTypeName());
	        
	        db2.deleteExperienceType(type.getTypeId());
	    	
	    }
	    
	    /*=================entry point of the testing program===========================*/
	    public static void main(String[] args) {
	        junit.textui.TestRunner.run(
	        		TestExperienceDaoImpl.class);
	    }
}
